﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Bonus_RatioPara。
    /// </summary>
    public class t_Bonus_RatioPara
    {
        public t_Bonus_RatioPara()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.t_Bonus_RatioPara model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Bonus_RatioPara(");
            strSql.Append("RecordId,CompanyId,AchievementsMin,AchievementsMax,Ratio,Deduct,AchievementYear,AchievementMonth)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@CompanyId,@AchievementsMin,@AchievementsMax,@Ratio,@Deduct,@AchievementYear,@AchievementMonth)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "AchievementsMin", DbType.Decimal, model.AchievementsMin);
            db.AddInParameter(dbCommand, "AchievementsMax", DbType.Decimal, model.AchievementsMax);
            db.AddInParameter(dbCommand, "Ratio", DbType.Decimal, model.Ratio);
            db.AddInParameter(dbCommand, "Deduct", DbType.Decimal, model.Deduct);
            db.AddInParameter(dbCommand, "AchievementYear", DbType.Int32, model.AchievementYear);
            db.AddInParameter(dbCommand, "AchievementMonth", DbType.Int32, model.AchievementMonth);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.t_Bonus_RatioPara model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Bonus_RatioPara set ");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("AchievementsMin=@AchievementsMin,");
            strSql.Append("AchievementsMax=@AchievementsMax,");
            strSql.Append("Ratio=@Ratio,");
            strSql.Append("Deduct=@Deduct,");
            strSql.Append("AchievementYear=@AchievementYear,");
            strSql.Append("AchievementMonth=@AchievementMonth");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "AchievementsMin", DbType.Decimal, model.AchievementsMin);
            db.AddInParameter(dbCommand, "AchievementsMax", DbType.Decimal, model.AchievementsMax);
            db.AddInParameter(dbCommand, "Ratio", DbType.Decimal, model.Ratio);
            db.AddInParameter(dbCommand, "Deduct", DbType.Decimal, model.Deduct);
            db.AddInParameter(dbCommand, "AchievementYear", DbType.Int32, model.AchievementYear);
            db.AddInParameter(dbCommand, "AchievementMonth", DbType.Int32, model.AchievementMonth);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Bonus_RatioPara ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Bonus_RatioPara GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CompanyId,AchievementsMin,AchievementsMax,Ratio,Deduct,AchievementYear,AchievementMonth from t_Bonus_RatioPara ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.t_Bonus_RatioPara model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CompanyId,AchievementsMin,AchievementsMax,Ratio,Deduct,AchievementYear,AchievementMonth ");
            strSql.Append(" FROM t_Bonus_RatioPara ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_Bonus_RatioPara");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.t_Bonus_RatioPara> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CompanyId,AchievementsMin,AchievementsMax,Ratio,Deduct,AchievementYear,AchievementMonth ");
            strSql.Append(" FROM t_Bonus_RatioPara ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.t_Bonus_RatioPara> list = new List<Chenxin.Lib.Business.Entity.t_Bonus_RatioPara>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 执行SQL（返回影响行数）
        /// </summary>
        public static bool ExecuteSQL(string strSQL)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            int count = db.ExecuteNonQuery(CommandType.Text, strSQL);
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Bonus_RatioPara ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.t_Bonus_RatioPara model = new Chenxin.Lib.Business.Entity.t_Bonus_RatioPara();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            ojb = dataReader["AchievementsMin"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AchievementsMin = (decimal)ojb;
            }
            ojb = dataReader["AchievementsMax"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AchievementsMax = (decimal)ojb;
            }
            ojb = dataReader["Ratio"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Ratio = (decimal)ojb;
            }
            ojb = dataReader["Deduct"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Deduct = (decimal)ojb;
            }
            ojb = dataReader["AchievementYear"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AchievementYear = (int)ojb;
            }
            ojb = dataReader["AchievementMonth"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AchievementMonth = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}